# -*- coding: utf-8 -*-
# vim:set noet ts=4:
#
# scim-python
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA
#
# $Id: $
#
import scim
import gtk 
from gtk import gdk
import os
import sys

try:
	from PYDict import *
except:
	pathname = os.path.dirname (__file__)
	path = os.path.abspath(pathname)
	path = os.path.join (path, "../../engine/PinYin")
	path = os.path.abspath(path)
	sys.path.append(path)
	from PYDict import *

from gettext import dgettext
_ = lambda a : dgettext ("scim-python", a)

RGB_COLOR = lambda c : ((c.red & 0xff00) << 8) + (c.green & 0xff00) + ((c.blue & 0xff00) >> 8)
GDK_COLOR = lambda c : gdk.Color (((c >> 16) & 0xff) * 256, ((c >> 8) & 0xff) * 256, (c & 0xff) * 256)
RGB = lambda r, g, b : ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)

class PinYinSetupUI (gtk.VBox):
	
	def __init__ (self):
		gtk.VBox.__init__ (self)
		
		# init value
		self._shuangpin_schema = "MSPY"
		self._fuzzy_pinyin = False
		self._auto_correct = True
		self._spell_check = True
		self._page_size = 5
		
		self._phrase_color			= RGB (0, 0, 0)
		self._user_phrase_color		= RGB (0, 0, 0xef)
		self._new_phrase_color		= RGB (0xef, 0, 0)
		self._special_phrase_color	= RGB (0, 0xbf, 0)
		self._english_phrase_color	= RGB (0, 0xbf, 0)
		self._error_eng_phrase_color	= RGB (0xef, 0, 0)
		
		self._create_ui ()
		self.show_all ()
		
	def _create_ui (self):
		
		line = 0	
		table = gtk.Table (3, 10)

		# Create combobox for Shuang Pin Schemas
		label = gtk.Label (_("ShuangPin Schema"))
		table.attach (label, 0, 1, line, line + 1)
		self._combo_schema = gtk.combo_box_new_text ()
		table.attach (self._combo_schema, 1, 2, line, line + 1)
		i = 0
		self._schemas = []
		for schema in SHUANGPIN_SCHEMAS.keys ():
			self._combo_schema.append_text(_(schema))
			self._schemas.append (schema)
			i += 1
		self._combo_schema.set_active (0)
		line += 1

		# Create combobox for The number for lookup table page size
		label = gtk.Label (_("Lookup table page size"))
		table.attach (label, 0, 1, line, line + 1)
		self._combo_page_size = gtk.combo_box_new_text ()
		table.attach (self._combo_page_size, 1, 2, line, line + 1)
		for i in xrange (1, 10):
			self._combo_page_size.append_text (str (i))
		self._combo_page_size.set_active (0)
		line += 1

		# Create checkbox for MoHu PinYin
		label = gtk.Label (_("Enable fuzzy PinYin support"))
		table.attach (label, 0, 1, line, line + 1)
		self._button_fuzzy_pinyin = gtk.CheckButton ()
		table.attach (self._button_fuzzy_pinyin, 1, 2, line, line + 1)
		line += 1

		# Create Autocorrect wrong PinYin
		label = gtk.Label (_("Enable wrong PinYin auto correct"))
		table.attach (label, 0, 1, line, line + 1)
		self._button_auto_correct = gtk.CheckButton ()
		table.attach (self._button_auto_correct, 1, 2, line, line + 1)
		line += 1
		
		# Create English spelling checking
		label = gtk.Label (_("Enable English spell check"))
		table.attach (label, 0, 1, line, line + 1)
		self._button_spell_check = gtk.CheckButton ()
		table.attach (self._button_spell_check, 1, 2, line, line + 1)
		line += 1

		# Create Colors choices
		self._button_colors = []
		self.pack_start (table)
		i = 0
		colors = (_("Color of Normal Phrases"), _("Color of New Phrase"), 
			_("Color of User Phrase"), _("Color of Special Phrase"),
			_("Color of English Candidates"), _("Color of Spelling Error"))
		for color in colors:
			label = gtk.Label (color)
			table.attach (label, 0, 1, line, line + 1)
			button_color = gtk.ColorButton ()
			table.attach (button_color, 1, 2, line, line + 1)
			self._button_colors.append (button_color)
			i += 1
			line += 1



	def get_name (self):
		return _("Python Pin Yin")
	
	def save_config (self, config):
		
		self._shuangpin_schema = self._schemas [self._combo_schema.get_active ()]
		self._fuzzy_pinyin = self._button_fuzzy_pinyin.get_active ()
		self._auto_correct = self._button_auto_correct.get_active ()
		self._spell_check = self._button_auto_correct.get_active ()
		self._page_size = self._combo_page_size.get_active () + 1
		
		self._phrase_color = RGB_COLOR (self._button_colors[0].get_color())
		self._new_phrase_color = RGB_COLOR (self._button_colors[1].get_color())
		self._user_phrase_color = RGB_COLOR (self._button_colors[2].get_color())
		self._special_phrase_color = RGB_COLOR (self._button_colors[3].get_color())
		self._english_phrase_color = RGB_COLOR (self._button_colors[4].get_color())
		self._error_eng_phrase_color = RGB_COLOR (self._button_colors[5].get_color())

		config.write ("/IMEngine/Python/PinYin/ShuangPinSchema", self._shuangpin_schema)
		config.write ("/IMEngine/Python/PinYin/FuzzyPinYin", self._fuzzy_pinyin)
		config.write ("/IMEngine/Python/PinYin/AutoCorrect", self._auto_correct)
		config.write ("/IMEngine/Python/PinYin/SpellCheck", self._spell_check)
		config.write ("/IMEngine/Python/PinYin/PageSize", self._page_size)
		
		# write colors
		config.write ("/IMEngine/Python/PinYin/PhraseColor", self._phrase_color)
		config.write ("/IMEngine/Python/PinYin/NewPhraseColor", self._new_phrase_color)
		config.write ("/IMEngine/Python/PinYin/UserPhraseColor", self._user_phrase_color)
		config.write ("/IMEngine/Python/PinYin/SpecialPhraseColor", self._special_phrase_color)
		config.write ("/IMEngine/Python/PinYin/EnglishPhraseColor", self._english_phrase_color)
		config.write ("/IMEngine/Python/PinYin/ErrorEnglishPhraseColor", self._error_eng_phrase_color)


	def load_config (self, config):
		# read values
		self._shuangpin_schema = \
			config.read ("/IMEngine/Python/PinYin/ShuangPinSchema", "MSPY")
		if self._shuangpin_schema not in self._schemas:
			self._shuangpin_schema = "MSPY"
		self._fuzzy_pinyin = \
			config.read ("/IMEngine/Python/PinYin/FuzzyPinYin", False)
		self._auto_correct = \
			config.read ("/IMEngine/Python/PinYin/AutoCorrect", True)
		self._spell_check = \
			config.read ("/IMEngine/Python/PinYin/SpellCheck", True)
		self._page_size = \
			config.read ("/IMEngine/Python/PinYin/PageSize", 5)
		if self._page_size < 1 or self._page_size > 9:
			self._page_size = 5
		self._phrase_color = \
			config.read ("/IMEngine/Python/PinYin/PhraseColor", self._phrase_color)
		self._new_phrase_color = \
			config.read ("/IMEngine/Python/PinYin/NewPhraseColor", self._new_phrase_color)
		self._user_phrase_color = \
			config.read ("/IMEngine/Python/PinYin/UserPhraseColor", self._user_phrase_color)
		self._special_phrase_color = \
			config.read ("/IMEngine/Python/PinYin/SpecialPhraseColor", self._special_phrase_color)
		self._english_phrase_color = \
			config.read ("/IMEngine/Python/PinYin/EnglishPhraseColor", self._english_phrase_color)
		self._error_eng_phrase_color = \
			config.read ("/IMEngine/Python/PinYin/ErrorEnglishPhraseColor", self._error_eng_phrase_color)
	
		# set widgets
		self._combo_schema.set_active (self._schemas.index (self._shuangpin_schema))
		self._button_fuzzy_pinyin.set_active (self._fuzzy_pinyin)
		self._button_auto_correct.set_active (self._auto_correct)
		self._button_spell_check.set_active (self._spell_check)
		self._combo_page_size.set_active (self._page_size - 1)
		
		self._button_colors[0].set_color (GDK_COLOR (self._phrase_color))
		self._button_colors[1].set_color (GDK_COLOR (self._new_phrase_color))
		self._button_colors[2].set_color (GDK_COLOR (self._user_phrase_color))
		self._button_colors[3].set_color (GDK_COLOR (self._special_phrase_color))
		self._button_colors[4].set_color (GDK_COLOR (self._english_phrase_color))
		self._button_colors[5].set_color (GDK_COLOR (self._error_eng_phrase_color))

	def query_changed (self):
		if self._schemas.index (self._shuangpin_schema) != self._combo_schema.get_active ():
			return True
		if self._fuzzy_pinyin != self._button_fuzzy_pinyin.get_active ():
			return True
		if self._auto_correct != self._button_auto_correct.get_active ():
			return True
		if self._spell_check != self._button_spell_check.get_active ():
			return True
		if self._page_size != self._combo_page_size.get_active () + 1:
			return True
		
		if self._phrase_color != RGB_COLOR (self._button_colors[0].get_color()):
			return True
		if self._new_phrase_color != RGB_COLOR (self._button_colors[1].get_color()):
			return True
		if self._user_phrase_color != RGB_COLOR (self._button_colors[2].get_color()):
			return True
		if self._special_phrase_color != RGB_COLOR (self._button_colors[3].get_color()):
			return True
		if self._english_phrase_color != RGB_COLOR (self._button_colors[4].get_color()):
			return True
		if self._error_eng_phrase_color != RGB_COLOR (self._button_colors[5].get_color()):
			return True
		
		return False

if __name__ == "__main__":
	window = gtk.Window ()
	setupui = PinYinSetupUI ()
	window.add (setupui)
	window.show_all ()
	print setupui.query_changed ()
	gtk.main ()
